package com.salton123.compat;

import android.Manifest;
import android.widget.Toast;

import com.salton123.feature.IFeature;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import androidx.fragment.app.FragmentActivity;
import io.reactivex.functions.Consumer;

/**
 * User: newSalton@outlook.com
 * Date: 2019/6/1 16:41
 * ModifyTime: 16:41
 * Description:
 */
public class PermissionFeatureCompat implements IFeature {
    private String[] permissions = getPermissionArr();

    public String[] getPermissionArr() {
        return new String[]{
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.WAKE_LOCK
        };
    }

    RxPermissions mRxPermissions;
    public FragmentActivity mHostAty;

    public PermissionFeatureCompat(FragmentActivity host) {
        mHostAty = host;
    }

    @Override
    public void onBind() {
        mRxPermissions = new RxPermissions(mHostAty);
        mRxPermissions.requestEachCombined(permissions).subscribe(new Consumer<Permission>() {
            @Override
            public void accept(Permission permission) throws Exception {
                if (!permission.granted) {
                    Toast.makeText(mHostAty,
                            "请授予全部权限",
                            Toast.LENGTH_LONG).show();
                    mHostAty.finish();
                }
            }
        });
    }

    @Override
    public void onUnBind() {

    }
}
